package com.card.manager.services.impl;

import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.AbbMapper;
import com.card.manager.domain.Abb;
import com.card.manager.domain.AbbExample;
import com.card.manager.domain.AbbExample.Criteria;
import com.card.manager.services.AbbService;

@Service("AbbServiceImpl")
public class AbbServiceImpl extends MyFrameworkSupperMapper implements AbbService {

	@Override
	public Map<String, Object> selectAbbForPage(Map<String, String> params,
			PageModel pageModel) {
		AbbMapper mapper = this.getMapper(AbbMapper.class);
		AbbExample abbExample = new AbbExample("b","a","c");
		//AabExample.createCriteria().andUsernameIsNotNull();
		//AabExample.createCriteria().andUsernameEqualTo(Aab.getUsername());
		if(pageModel.getRows() != 0)
			abbExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
			abbExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
		
		Criteria criteria = abbExample.createCriteria();
		criteria.andFlagEqualTo((byte)1);
		if(params != null){
			if(StringUtils.isNotEmpty(params.get("ownercard"))){
				criteria.andAaaOwnercardEqualTo(params.get("ownercard"));
			}
			if(StringUtils.isNotEmpty(params.get("abaid"))){
				criteria.andAbaidEqualTo(params.get("abaid"));
			}
			if(StringUtils.isNotEmpty(params.get("id"))){
				criteria.andIdEqualTo(Long.parseLong(params.get("id")));
			}
			if(StringUtils.isNotEmpty(params.get("creatdatefrom"))){
				String dateStr = params.get("creatdatefrom");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date;
				try {
					date = sdf.parse(dateStr);
					criteria.andCreatdatetimeGreaterThanOrEqualTo(date);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if(StringUtils.isNotEmpty(params.get("creatdateto"))){
				String dateStr = params.get("creatdateto");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date;
				try {
					date = sdf.parse(dateStr);
					criteria.andCreatdatetimeLessThanOrEqualTo(date);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if(StringUtils.isNotEmpty(params.get("creatuserid"))){
				criteria.andCreatuseridEqualTo(params.get("creatuserid"));
			}
		}
		
		
		List<Abb> list = mapper.selectByExampleFetchJoin(abbExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    abbExample.setPage(null);
	    List<Abb> footer = mapper.sumByExampleFetchJoin(abbExample);
	    modelMap.put("footer", footer); 
	    return modelMap;
	}

}
